//
//  TheNewAddressTableViewController.m
//  BuyWhat
//
//  Created by ios on 2016/10/31.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "TheNewAddressTableViewController.h"
#import "TheNewAddressTableViewCell.h"

@interface TheNewAddressTableViewController ()<UITableViewDelegate , UITableViewDataSource>

@property (nonatomic , strong) NSMutableArray *dataArray;
Strong UITableView *tableView;

@end

//网络
@implementation TheNewAddressTableViewController (NetWorking)

//修改用户地址信息
- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters;
    NSString *url;
    if (self.type == kTheNewAddressTypeAdd) {
        url = @"LaundryMall/Login_Registration/newUserAddress.action";
       parameters = @{@"auId":[UserModel sharedInstanced].userId,
                      @"ua_name":self.dataArray[0][@"text"],
                      @"ua_mobile":self.dataArray[1][@"text"],
                      @"ua_address":self.dataArray[2][@"text"]};
    }else{
        parameters = @{@"ua_id":self.model[@"ua_id"],
                       @"ua_name":self.dataArray[0][@"text"],
                       @"ua_mobile":self.dataArray[1][@"text"],
                       @"ua_address":self.dataArray[2][@"text"]};
        url = @"LaundryMall/Login_Registration/modifyAddressInformation.action";
    }
    [[AFAppDotNetAPIClient sharedClient] POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [self.view makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation TheNewAddressTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = (self.type == kTheNewAddressTypeAdd ? @"新增地址" : @"编辑地址");    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
    [self.view addSubview:self.tableView];
    [self setupForDismissKeyboard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"eeeeee");
    }
    return _tableView;
}

#pragma mark -  set

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.dataArray[0][@"text"] = model[@"ua_name"];
    self.dataArray[1][@"text"] = model[@"ua_mobile"];
    self.dataArray[2][@"text"] = model[@"ua_address"];
    [self.tableView reloadData];
}

#pragma mark - click

- (void)rightBarButtonItemClick{
    NSLog(@"完成");
    
    [self.view endEditing:YES];
    for (int i = 0; i < self.dataArray.count; i ++) {
        if ([self.dataArray[i][@"text"] isEqualToString:@""]) {
            [self showMessage:self.dataArray[i][@"title"]];
            return;
        }
    }
    [self getData];
}

#pragma mark - <UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45 / WIDTH_5S_SCALE;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TheNewAddressTableViewCell *cell = [TheNewAddressTableViewCell cellForTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    cell.getTextField = ^(NSString *str) {
        self.dataArray[indexPath.row][@"text"] = str;
        [self.tableView reloadData];
    };
    return cell;
}

#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@{@"image":@"icon_name",@"title":@"请输入姓名",@"text":@""}.mutableCopy,
                       @{@"image":@"icon_phone",@"title":@"请输入手机号码",@"text":@""}.mutableCopy,
                       @{@"image":@"icon_add_address",@"title":@"请输入详细地址",@"text":@""}.mutableCopy].mutableCopy;
    }
    return _dataArray;
}

@end
